﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets
{
    public class BrushButton : Border
    {
        public BrushButton(BrushItem bi)
        {
            this.brushItem = bi;
            if (bi == null)
            {
                bi = BrushManager.BrushesList[0];
            }

            this.ToolTip = bi.CnName + "/" + bi.EnName;

            this.BorderThickness = new System.Windows.Thickness(1);
            this.BorderBrush = Brushes.Black;
            this.Margin = new System.Windows.Thickness(1);
            this.Height = 20;
            this.Width = 20;
            this.Background = Brushes.White;

            MouseEnter += BrushButton_MouseEnter;
            MouseLeave += BrushButton_MouseLeave;
            MouseLeftButtonDown += BrushButton_MouseLeftButtonDown;
        }

        private BrushItem brushItem;

        public BrushItem BrushItem { get { return brushItem; } }

        private void BrushButton_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                var ate = Globals.MainWindow.ActiveTextEditor;
                if (ate == null) return;

                var line = ate.Document.GetLineByOffset(ate.SelectionStart);
                var left = ate.Document.GetText(line.Offset, ate.SelectionStart - line.Offset);

                var colorName = brushItem.CnName + ", ";

                if (left.Length > 0)
                {
                    var regexStart = new Regex(@"^[ 　]{0,3}((\[[ 　\t]{0,}?[\$￥])|([;；][ 　\t]{0,}?.*[:：][ 　\t]{0,}?))");
                    var matchStart = regexStart.Match(left);
                    if (matchStart.Success)
                    {
                        var regexEnd = new Regex(@"[,，][ 　\t]{0,}$");  // 以逗号结尾
                        var matchEnd = regexEnd.Match(left);
                        if (matchEnd.Success == false)
                        {
                            colorName = ", " + colorName;
                        }
                    }
                }

                ate.SelectedText = colorName;
                ate.Select(ate.SelectionStart + colorName.Length, 0);
            }
            catch (Exception ex)
            {
                LMessageBox.Show(ex.Message);
            }
        }

        private void BrushButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.BorderBrush = Brushes.Black;
        }

        private void BrushButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.BorderBrush = BrushManager.GetBrush("浅棕色");
        }

        public Brush Fill
        {
            get { return this.Background; }
            set { this.Background = value; }
        }
    }
}
